//
//  IndividualViewController.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "IndividualViewController.h"
#import "SettingController.h"
#import "Header.h"

#import "CustomView.h"

#import <QuartzCore/QuartzCore.h>

#import "UIButton+WebCache.h"

#import "collectCell.h"
#import "LandController.h"
#import "ThirdNaviSearchController.h"

#import "IndividualEmptyCell.h"

#import "QuickLookViewController.h"
@interface IndividualViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSNumber*  _number;
    BOOL isLoad;
    BOOL _isLand;

    
}

@property(nonatomic,strong)NSMutableArray* TopicsArray;

@property(nonatomic,strong)NSMutableArray* ComicsArray;

@property(nonatomic,strong)CustomView* tableHeaderView;
@property (nonatomic,assign)JZJTableViewState currentState;

@end

@implementation IndividualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"个人";

    [self netState];

}
- (void)netState{

    
    [self createUI];
    //个人页 关注的专题
    
    
    if (_isLand) {
        [self FavTopicsLoadData];
        
        //个人页 收藏单篇
        
    }
    //

}


- (void)FavTopicsLoadData
{

    
    //如果没网 直接停止动画
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    BOOL isConnection;
    
    isConnection=[userdefault boolForKey:@"isConnection"];
    
    if (!isConnection) {
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        
    }
    
    //关注的专题   个人页
    //http://api.kuaikanmanhua.com/v1/fav/topics?limit=20&offset=0
    
    
    

    
    [DownLoad getIndividualViewControllerFavTopicsWithoffset:_number andBlock:^(id obj, NSError *error) {
        
        
        _TopicsArray=[NSMutableArray array];

        
        JZJLog(@"%zd",_TopicsArray);
        NSArray* comics= obj[@"data"][@"topics"];
        
        //开始循环
        
        
        [comics enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
            
            
            
            
            Model* model=[[Model alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            [_TopicsArray addObject:model];
            
        }];
        
        
        [_tableView reloadData];
        isLoad=NO;//这是一个异步执行方法 当这个异步操作执行完毕之后 isLoad才会变成NO
        [self.tableView.header endRefreshing];
  }];
   
    
   

}




- (void)viewWillAppear:(BOOL)animated{
    
    
    
    
//    JZJLog(@"xxx%zd",_TopicsArray.count);

    [super viewWillAppear:YES];
    

    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    BOOL isLand= [userdefault boolForKey:ISLAND];
    
    _isLand=isLand;
    JZJLog(@"xxx%@",_isLand?@"已经登录":@"没有登录");

    _ComicsArray=[NSMutableArray array];
    _TopicsArray=[NSMutableArray array];

    [_tableView reloadData];

    [self refreshTableHeaderView];

    if (_isLand) {
        
//        //收藏专题
        [self FavTopicsLoadData];

    }
        else
    {

        
        
        
        _ComicsArray=[NSMutableArray array];
        _TopicsArray=[NSMutableArray array];
        [_tableView reloadData];

    }
    

    
}

- (void)refreshTableHeaderView{


    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    NSDictionary* dic= [userdefault objectForKey:DATADIC];
    
    [_tableHeaderView refreshIndividualTableHeaderViewWithDic:dic];
    



}
- (void)createUI
{
    
    
    
    
    //初始化参数
    _number=@0;
    
    
    //去掉backbarbutton文字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    
    
    //右侧文字设置
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStyleDone target:self action:@selector(push)];
    
    
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-49)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    _tableView.showsVerticalScrollIndicator=NO;
    
    _tableView.separatorColor=SEPARATORCOLOR;
    
    
    
    [self.view addSubview:_tableView];
    
    
    
//添加头视图
    _tableHeaderView=[[NSBundle mainBundle]loadNibNamed:@"IndividualXIB" owner:nil options:nil][0];
    
    [self refreshTableHeaderView];

    _tableView.tableHeaderView=_tableHeaderView;
      

  
    _tableHeaderView.IndividualViewControllerDelegate=self;
    //关注专题
    [_tableHeaderView.attentionTopic addTarget:self action:@selector(attentionTopic:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //关注单篇
//    [_tableHeaderView.collectItem addTarget:self action:@selector(collectItem:) forControlEvents:UIControlEventTouchUpInside];
    

    WS(weakSelf)
    [_tableView addLegendHeaderWithRefreshingBlock:^{
        
        [weakSelf FavTopicsLoadData];

    }];

}



//关注专题

- (void)attentionTopic:(UIButton*)attentionTopic
{

    
    [UIView animateWithDuration:0.1 animations:^{
        
        CGRect frame=_tableHeaderView.slideView.frame;
        
        _tableHeaderView.slideView.frame=CGRectMake(0,_tableHeaderView.frame.size.height-3, frame.size.width, frame.size.height);
 
        
        
    }];
    
    
    if (_isLand) {
        
        self.currentState = JZJTableViewState_showIntroduce;
        
        [self FavTopicsLoadData];

        
    }
    
    
    
    

}





//登录跳转


- (void)land{

    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    

    if ([userdefault boolForKey:ISLAND]!=YES) {
        
        LandController* vc=[[LandController alloc]init];
        
        vc.hidesBottomBarWhenPushed=YES;
        
        
        [self.navigationController pushViewController:vc animated:YES];
        

    }

    
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
  
    if (_isLand)
    {
        
        if (_TopicsArray.count==0)
        {
            
            
            return 1;
        
        }else{
        
        
            return _TopicsArray.count;
        }
            
            
       
        
        
    }
    
    else
    {
        
        return 1;
        
        
    }
    
    
    
    return 0;
}
//FIVEANGLE

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isLand) {
        
    
    
    if (_TopicsArray.count) {

    collectCell * cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (!cell) {
        cell=[[NSBundle mainBundle]loadNibNamed:@"collectCell" owner:nil options:nil][0];
        
        cell.selectionStyle=UITableViewCellEditingStyleNone;
        
        
        
        //tableview去掉多余线 cell
        _tableView.tableFooterView = [[UIView alloc]init];
        
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
        
        [self.tableView setTableFooterView:v];
        
    }
        
    Model* model=_TopicsArray[indexPath.row];
            
            
    [cell refreshTopicsWithModel:model];
  
        
    return cell;

    }
    else if(_TopicsArray.count==0)
        {
    
    
        
        
            IndividualEmptyCell* cell=[tableView dequeueReusableCellWithIdentifier:@"IndividualEmptyCell"];
            
            
            if (!cell) {
                cell=[[NSBundle mainBundle]loadNibNamed:@"IndividualEmptyCell" owner:nil options:nil][0];
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
            
            cell.textLabel.text=@"去专题页面的右上角,点击关注吧";
            
            return cell;
            
            
            
   
    
        }
        
    }
    else if(!_isLand)
    {
        IndividualEmptyCell* cell=[tableView dequeueReusableCellWithIdentifier:@"IndividualEmptyCell"];
    
        
        if (!cell) {
            cell=[[NSBundle mainBundle]loadNibNamed:@"IndividualEmptyCell" owner:nil options:nil][0];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        
        return cell;
        
        
    
    }
    
    
    return nil;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (_isLand&&_TopicsArray.count)
    {
        
      
            
            return  100;
         }
   else
    {
        return SCREEN_HEIGHT-64-_tableHeaderView.frame.size.height-49;
    
    }
    return 0;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_TopicsArray.count==0) {

        QuickLookViewController* vc=[[QuickLookViewController alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
    
    }
    

    if (_isLand&&_TopicsArray.count) {
        
        if (_TopicsArray.count) {
            
            
            
            
            Model* model=_TopicsArray[indexPath.row];
            
            ThirdNaviSearchController* vc=[[ThirdNaviSearchController alloc]init];
            
            vc.model=model;

            
            [self.navigationController pushViewController:vc animated:YES];
            
            
            
            
            
            
            
        }else
        {
            
            
            
            Model* model=_ComicsArray[indexPath.row];
            
            WebViewController* web=[[WebViewController alloc]init];
            web.hidesBottomBarWhenPushed=YES;
            web.model=model;
            
            [self.navigationController pushViewController:web animated:YES];
            
            
        }
        

    }
    
    
}


//rightbarbutton跳转设置页面

- (void)push {
    
    SettingController* vc=[[SettingController alloc]init];
    
    vc.dataDic=_dataDic;
    
    [self.navigationController pushViewController:vc animated:YES];
  
}










- (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return @"删除";
}



- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (!_TopicsArray.count) {
        return UITableViewCellEditingStyleNone;
    }
    
    return UITableViewCellEditingStyleDelete;
}




//点击增加 或者是删除时 所触发的方法 不管增加还是删除都会触发这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    if (_currentState==JZJTableViewState_showIntroduce) {
        
        
        
        Model* model=_TopicsArray[indexPath.row];
        
        [DownLoad delteComicsWithID:model.id andBlock:^(id obj, NSError *error) {
            
            
            
            if ([obj[@"message"]isEqualToString:@"OK"]) {
                Model* model=_TopicsArray[indexPath.row];
                
                
                NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
                
                NSArray* array=  [userdefault objectForKey:IDArray];
                
                
                if (array.count) {
                    NSMutableArray* mArray=[NSMutableArray array];
                    
                    [mArray arrayByAddingObjectsFromArray:array];
                    
                    [mArray removeObject:model.id];
                    
                    
                    [userdefault setObject:mArray forKey:IDArray];
                    [userdefault synchronize];
                    JZJLog(@"%@",model.id);
                    
                    
                }


                //请求成功 删除数据
                [_TopicsArray removeObjectAtIndex:indexPath.row];
          

                
                
                [tableView reloadData];

            }

                   
        }];
        
        
    }
    
    
    
    
    
    
    
}

@end
